set -e
set -o pipefail

if [ -z "${BATCH_PATTERN}" ]; then BATCH_PATTERN="glob:**"; fi
if [ -z "${AMNESTIC}" ]; then AMNESTIC="false"; fi
if [ -z "${RESUME}" ]; then RESUME="true"; fi
if [ -z "${USE_APPROACH_BASELINE_FOR_RETRACTION}" ]; then USE_APPROACH_BASELINE_FOR_RETRACTION="false"; fi
if [ -z "${SVG_PATTERN}" ]; then SVG_PATTERN="glob:**0.001*"; fi
if [ -z "${WATCH}" ]; then WATCH="false"; fi
if [ -z "${APPROACH_SKIP_DISTANCE}" ]; then APPROACH_SKIP_DISTANCE=150; fi
if [ -z "${THREADS}" ]; then THREADS=1; fi
if [ -z "${FILE_FORMAT}" ]; then FILE_FORMAT="nanoscope"; fi
if [ -z "${TEMPERATURE}" ]; then TEMPERATURE=301.15; fi
if [ -z "${MIN_PEAK_FORCE_PN}" ]; then MIN_PEAK_FORCE_PN="(50.1,40),(4360,65)"; fi
if [ -z "${MIN_PEAK_FORCE_RELATIVE_TO_EMPIRICAL_BASELINE_PN}" ]; then MIN_PEAK_FORCE_RELATIVE_TO_EMPIRICAL_BASELINE_PN=0; fi
if [ -z "${MEDIAN_ABSOLUTE_DEVIATION_LOWER_BOUND_PN}" ]; then MEDIAN_ABSOLUTE_DEVIATION_LOWER_BOUND_PN="(50.1,5),(4360,22)"; fi
if [ -z "${LEFT_MOSTLY_HIGHER_WINDOW_SIZE_NM}" ]; then LEFT_MOSTLY_HIGHER_WINDOW_SIZE_NM=6.1; fi
if [ -z "${LEFT_MOSTLY_HIGHER_FRACTION}" ]; then LEFT_MOSTLY_HIGHER_FRACTION=0.7; fi
if [ -z "${LEFT_MOSTLY_HIGHER_PN}" ]; then LEFT_MOSTLY_HIGHER_PN=1.0; fi
if [ -z "${RIGHT_MUCH_HIGHER_THAN_ALL_PN}" ]; then RIGHT_MUCH_HIGHER_THAN_ALL_PN="(50.1,5),(4360,5)"; fi
if [ -z "${RIGHT_MUCH_HIGHER_PN}" ]; then RIGHT_MUCH_HIGHER_PN="(50.1,23),(4360,60)"; fi
if [ -z "${AT_MOST_ONE_PEAK_WINDOW_NM}" ]; then AT_MOST_ONE_PEAK_WINDOW_NM=5; fi
if [ -z "${MIN_CONTOUR_LENGTH_DELTA_NM}" ]; then MIN_CONTOUR_LENGTH_DELTA_NM=5; fi
if [ -z "${MIN_PEAK_CONTOUR_LENGTH}" ]; then MIN_PEAK_CONTOUR_LENGTH=10; fi
if [ -z "${SLOPE}" ]; then SLOPE=""; fi
if [ -z "${SVG_DEF_SCALE}" ]; then SVG_DEF_SCALE=1.0; fi
if [ -z "${SVG_ITERATION_OFFSET_PN}" ]; then SVG_ITERATION_OFFSET_PN=200.0; fi
if [ -z "${ITERATION}" ]; then ITERATION=0; fi
if [ -z "${ALLOWED_TIME_RELATIVE_ERROR}" ]; then ALLOWED_TIME_RELATIVE_ERROR="0.06,0.8,0.18,0.8,0.04,0.8,0.04"; fi

# BATCH_CLASSPATH for debugging: mvn dependency:build-classpath plus target/classes
if [ -z "${BATCH_CLASSPATH}" ]; then BATCH_CLASSPATH="target/refolding-core-0.1-jar-with-dependencies.jar"; fi

fixedDeflectionSensitivityNmPerV=$DEFL_SENS; fixedSpringConstantNPerM=$K; for d in $DIRS; do p=$(dirname ${d}); b=$(basename ${d})${fixedDeflectionSensitivityNmPerV//*/_${fixedDeflectionSensitivityNmPerV}}${fixedSpringConstantNPerM//*/_${fixedSpringConstantNPerM}}; cd ~/work/rcs/refolding-read-only/core && nice -n 10 java -ea -cp ${BATCH_CLASSPATH} -Djava.awt.headless=true it.unibo.refolding.pico.BatchApproachRetractionAnalysis --iteration=${ITERATION} --svgDefScale=${SVG_DEF_SCALE} --svgIterationOffsetPn=${SVG_ITERATION_OFFSET_PN} --fileFormat=${FILE_FORMAT} --laxApproachContactPoint=true --useApproachBaselineForRetraction=${USE_APPROACH_BASELINE_FOR_RETRACTION} --threads=${THREADS} --defLsbVsZLsbSlope=${SLOPE} --minPeakForcePn=${MIN_PEAK_FORCE_PN} --minPeakForceRelativeToEmpiricalBaselinePn=${MIN_PEAK_FORCE_RELATIVE_TO_EMPIRICAL_BASELINE_PN} --medianAbsoluteDeviationLowerBoundPn=${MEDIAN_ABSOLUTE_DEVIATION_LOWER_BOUND_PN} --leftMostlyHigherWindowSizeNm=${LEFT_MOSTLY_HIGHER_WINDOW_SIZE_NM} --leftMostlyHigherFraction=${LEFT_MOSTLY_HIGHER_FRACTION} --leftMostlyHigherPn=${LEFT_MOSTLY_HIGHER_PN} --rightMuchHigherThanAllPn=${RIGHT_MUCH_HIGHER_THAN_ALL_PN} --rightMuchHigherPn=${RIGHT_MUCH_HIGHER_PN} --atMostOnePeakWindowNm=${AT_MOST_ONE_PEAK_WINDOW_NM} --minPeakContourLengthNm=${MIN_PEAK_CONTOUR_LENGTH} --minContourLengthDeltaNm=${MIN_CONTOUR_LENGTH_DELTA_NM} --dataDir=${d} --inputPathList= --wildcardPattern=${BATCH_PATTERN} --retractionPlaylist=${p}/analysis/${b}_analysis/${b}.repr --hookePlaylist=${p}/analysis/${b}_analysis/${b}.hkp --infoPath=${p}/analysis/${b}_analysis/${b}.info --svgDir=${p}/analysis/${b}_analysis/${b}_svg --resume=${RESUME} --contactPointAndSlopeDir=${p}/analysis/${b}_analysis/${b}_cps --peakSlopesDir=${p}/analysis/${b}_analysis/${b}_psi --peakForceStrategy=raw --temperatureK=${TEMPERATURE} --persistenceLengthNm=0.35 --clRangeStartNm=0 --clRangeEndNm=Infinity --wellSpacedAtEnd=0 --allowSkipped=0 --peakCsvPath=${p}/analysis/${b}_analysis/${b}_peaks.csv --watch=${WATCH} --fixedSvgPath=${FIXED_SVG_PATH} --svgWildcardPattern=${SVG_PATTERN} --approachSkipDistanceNm=${APPROACH_SKIP_DISTANCE} --junctionPointsDir=${p}/analysis/${b}_analysis/${b}_jp --filteredHookePlaylist= --filteredRetractionPlaylist= --fixedDeflectionSensitivityNmPerV=${fixedDeflectionSensitivityNmPerV} --fixedSpringConstantNPerM=${fixedSpringConstantNPerM} --amnestic=${AMNESTIC} --fetchRefoldBreakParams=${FETCH_REFOLD_BREAK_PARAMS} --loopParams=${LOOP_PARAMS} --allowedTimeRelativeError=${ALLOWED_TIME_RELATIVE_ERROR}; done
